package com.ygou.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygou.pojo.UserAddress;
import com.ygou.service.UserAddressService;

@Controller
@RequestMapping(value="/address")
public class UserAddressController {
	@Autowired
	private UserAddressService userAddressService;
	
	/**
	 * 根据用户编号获取用户地址集合
	 * @param userid
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getaddress/{userid}")
	public String getAllAddresssByUId(@PathVariable Integer userid,ModelMap model){
		List<UserAddress> address=userAddressService.findAddressByUId(userid);
		model.put("address",address);
		return "address";
	}
	/**
	 * 添加用户地址
	 * @param address
	 * @return
	 */
	@RequestMapping(value="/addAddress",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean addAddress(UserAddress address){
		return userAddressService.insert(address);
	}
	/**
	 * 修改用户地址
	 * @param address
	 * @return
	 */
	@RequestMapping(value="/updAddress",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean updAddress(UserAddress address){
		return userAddressService.updateByPrimaryKeySelective(address);
	}
	/**
	 * 删除用户地址
	 * @param aid
	 * @return
	 */
	@RequestMapping(value="/delAddress/{aid}",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean delAddress(@PathVariable Integer aid){
		return userAddressService.deleteByPrimaryKey(aid);
	}
	/**
	 * 根据地址id获取对应地址信息
	 * @param aid
	 * @return
	 */
	@RequestMapping(value="/addressInfo",produces="application/json;charset=UTF-8")
	public @ResponseBody UserAddress getAddress(Integer aid){
		return userAddressService.selectByPrimaryKey(aid);
	}
	/**
	 * 设置默认收货地址
	 * @param aid
	 * @return
	 */
	@RequestMapping(value="/setDefaultArs/{aid}",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean setDefaultArs(@PathVariable Integer aid){
		return userAddressService.updDefaultArs(aid);
	}
}
